package com.ruoyi.client.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.client.annotation.Client_oper;
import com.ruoyi.client.annotation.TargetJson;
import com.ruoyi.client.domain.entity.BlogLike;
import com.ruoyi.client.domain.vo.EchartsVo;
import com.ruoyi.client.eum.ClientBusinessType;
import com.ruoyi.client.service.IBlogLikeService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.web.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@RestController
@RequestMapping("client/user/like")
public class BlogLikeController extends BaseController {
    @Autowired
    IBlogLikeService likeService;
    @GetMapping("addLikeNum")
    public R<List<EchartsVo>> addLikeNum(){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
//        // 获取当前日期
//        LocalDate today = LocalDate.now();
//        // 获取当前月的第一天
//        LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
//        // 获取当前月的最后一天
//        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
//        // 获取当前月的开始时间（第一天的 00:00:00）
//        LocalDateTime startOfMonth = firstDayOfMonth.atStartOfDay();
//        // 获取当前月的结束时间（最后一天的 23:59:59.999999999）
//        LocalDateTime endOfMonth = lastDayOfMonth.atTime(LocalTime.MAX);

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取7天前的日期
        LocalDate sevenDaysAgo = today.minusDays(6); // 包括今天，所以减去6天
        // 获取最近7天的开始时间（7天前的 00:00:00）
        LocalDateTime startOfLastSevenDays = sevenDaysAgo.atStartOfDay();
        // 获取最近7天的结束时间（今天的 23:59:59.999999999）
        LocalDateTime endOfLastSevenDays = today.atTime(LocalTime.MAX);
        List<EchartsVo> result = likeService.addLikeNum(userId,startOfLastSevenDays,endOfLastSevenDays);
        return R.ok(result);
    }
    /**
     * 点赞
     * @param
     * @return
     */
    @PostMapping
    @Client_oper(title = "点赞",clientBusinessType = ClientBusinessType.TYPE_LIKE)
    public R likeArticle(@RequestBody @TargetJson BlogLike like){
        Long userId = Long.valueOf((String) StpUtil.getLoginId());
        like.setUserId(userId);
        likeService.likeArticle(like,like.getIsLike());
        return toAjax(true);
    }
}
